<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
${aier_doctype}
<html <s:property value="aier_html_xmlns" escape="false"/>>
<head>
<meta http-equiv="Content-Type" content="${aier_content_type}" />
<meta name="keywords" content="${aier_keyworks}" />
<meta name="description" content="${aier_description}" />
<title>${aier_title}</title>
<link rel="stylesheet" href="${aier_css_url}${aier_jqui_css}ui.css" />
<link rel="Stylesheet" href="${aier_css_url}jq/jq.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/jq-msg.css" />

<link rel="stylesheet" href="${aier_css_url}social/module/reset.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/btn.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/icon.css" />
<link rel="Stylesheet" href="${aier_css_url}social/module/emotion.css" />
<link rel="Stylesheet" href="${aier_css_url}social/module/homeside.css" />
<link rel="stylesheet" href="${aier_css_url}social/user/userMessage/usermessage.css" />
<link rel="stylesheet" href="${aier_css_url}social/module/xxs.css" />

<script type="text/javascript" src="${aier_js_url}j.js"></script>
<script type="text/javascript" src="${aier_js_url}b.js"></script>
<script type="text/javascript" src="${aier_js_url}${aier_jqui_js}ui.js"></script>
<script type="text/javascript" src="${aier_js_url}jp/msg.js"></script>
<script type="text/javascript" src="${aier_js_url}97/WdatePicker.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/cn.js"></script>
<script type="text/javascript" src="${aier_js_url}jq/jq.js"></script>
<script type="text/javascript" src="${aier_js_url}lib/block.js"></script>
<script type="text/javascript"
	src="${aier_js_url}emotion/xxsemotion.2.js"></script>
<script type="text/javascript" src="${aier_js_url}trantime/jquery-aier.js"></script>

</head>
<body>

	<!-- 顶栏 -->
	<s:action name="userhomecomm_homeHead" namespace="/user" executeResult="true"></s:action>
	<!-- 顶栏结束 -->


	<div class="window-content">

		<!-- wrap  -->
		
		<div class="wrap wrap-message wrap-msg wrap-msg-letter wrap-tri-col clearfix">

			<!-- 主侧栏 -->
			<s:action name="usermessage_msgSide" namespace="/user" executeResult="true"></s:action>


			<!-- 主区域 (右  -->
			<div class="msg-main clearfix">
				<div class="msg-stream">
					<div class="msg-title title">
						<div class="group">
							私信
						</div>
					</div>
					<!--  -->
					<div class="msg-title-function clearfix">
						<div class="function-left clearfix">
							<a class="btn btn-normal-orange" id="btnSendALetter">
								<span>发私信</span>
							</a>
							<!-- dialog -->
							<div id="dialogSendALetter" class="dialog-send-letter" title="发私信" style="display:none">
								<div class="nickbar">
									<label>
										<span>请输入对方昵称:</span>
										<input type="text" id = "nickname"/>
									</label>
								</div>
								<div class="textbar">
									<label>
										<span>请输入内容:</span>
										<textarea id = "content"></textarea>
									</label>
								
								</div>
								<div class="emotion-bar">
									<a class="btn-emotion">
										<i class="icon-1 icon-1-emotion"></i>
									</a>
								</div>
							</div>
							<a class="btn btn-normal-orange" id = "delete">
								<span>删除所有私信</span>
							</a>
						</div>

					</div>
					
					<div class="msg-list msg-list-letter clearfix" id = "letter">
						
					</div>
					<div id = "page"></div>

				</div>
				<div id="deleteConfirm" style="display: none;" title="删除">
	                <p>确认要删除所有私信吗?</p>
	            </div>

                <!-- 右栏 -->
                <s:action name="userhomecomm_homeRight" namespace="/user" executeResult="true"></s:action>
			</div>


			<!-- 返回顶部 -->
			<s:action name="userhomecomm_backToTop" namespace="/user" executeResult="true"></s:action>
			<!-- end 返回顶部 -->




		</div>
		<!-- footer copyright -->
		<s:action name="userhomecomm_homeFooter" namespace="/user" executeResult="true"></s:action>
		<!-- /footer copyright -->
	</div>



<script type="text/javascript">
var page = 1;       // 当前页
var page_size = 10; // 每页的记录数

// 转换时间
$(document).ready(function(){
	$('.msg-date').each(function(){
		$(this).tranTime();
	});
});
// 预览图和完整图切换
$('.zoom-out').click(function(){
	$(this).parents('.xxs-extend').css('display','none')
	.prev('.xxs-media-pre').css('display','block');
});
$('.zoom-in').click(function(){
	$(this).parents('.xxs-media-pre').css('display','none')
	.next('.xxs-extend').css('display','block');
});

$(window).bind('scroll', function() {
	var a = $(this).scrollTop(),
		b = $(this).outerHeight();
	if ( a > 100 ) {
		$('#btt').css({'display':'block','top':a+b-200});
	} else {
		$('#btt').css('display','none');
	}
});

$('#btt').click(function(){
	$('#changesrc').attr('src', '../images/upload/userCenter/12.jpg');
});

    $(document).ready(function() {
        // 获取关注分组
        var fun=function(data){
            if(data.s==1){
                var c = "";
                for (var i=0;i<data.fgList.length;i++) {
                    if (data.fgList[i].name == "特别关注") {
                        c += "<div class='tab-normal'><a class='btn' href='${aier_url}user/userhomeforums_homeForums.shtml?fgid="+data.fgList[i].fgid+"'><em class='icon-1 icon-1-teguan'></em><span>"+data.fgList[i].name+"</span></a></div>";
                    } else {
                        c += "<div class='tab-normal'><a class='btn' href='${aier_url}user/userhomeforums_homeForums.shtml?fgid="+data.fgList[i].fgid+"'><em class='icon-1 icon-1-weifen'></em><span>"+data.fgList[i].name+"</span></a></div>";
                    }
                    
                }
                $("#group").html(c);
                
            }
        };
        exeAjax(
             ajaxUrl("${aier_url}${aier_urijs}usermyfollow_searchFollowGroup${aier_suffix}"),
             fun,{
        });
        searchLetter(page);
    });
    
    function searchLetter(nowpage) {
    	page = nowpage;
        var fun=function(data){
            if(data.s==1){
                var html = "";
                if (data.letterList == null || data.letterList.length == 0) {
                    html += '<div style="margin:0 20px;">暂时没有私信！</div>';
                } else {
                    for (var i=0;i<data.letterList.length;i++) {
                        var letter = data.letterList[i];
                        
                        if (letter.type == 1) {
                        	html +='<div class="msg-item msg-item-letter" data-url="${aier_url}user/usermessage_userMessageLetterIn.shtml?lid='+letter.lid+'&&ruid='+letter.ruid+'&&suid='+letter.suid+'"><div class="msg-avatar"><a><img src="${get_picturl_url}small/'+letter.headimg+'" alt=""></a></div>';
                            html +='<div class="msg-content"><div class="username">'+letter.nickname+':</div><div class="text">'+letter.content+'</div></div>';
                            html += '<div class="msg-date"><span>'+letter.cdate.split("T")[0]+" "+letter.cdate.split("T")[1]+'</span></div></div>';
                        
                        } else if(letter.type == 3 || letter.type == 5) {
                        	html +='<div class="msg-item msg-item-letter msg-item-forchums"><div class="msg-avatar"><a><img src="${get_picturl_url}small/'+letter.headimg+'" alt=""></a></div>';
                            html +='<div class="msg-content"><div class="username">'+letter.nickname+':</div><div class="text">'+letter.content+'</div></div>';
                            html += '<div class="msg-date"><span>'+letter.cdate.split("T")[0]+" "+letter.cdate.split("T")[1]+'</span></div></div>';
                        } else if (letter.type == 2 || letter.type == 4){
                        	// 密友请求私信
                        	html += '<div class="msg-item msg-item-letter msg-item-forchums" data-type="'+letter.type+'" data-uid="'+letter.suid+'"><div class="msg-avatar"><a><img src="${get_picturl_url}small/'+letter.headimg+'" alt=""></a></div>';
                        	html += '<div class="msg-content"><div class="username">'+letter.nickname+'<span>'+letter.content.substring(0,letter.content.indexOf("。")+1)+'</span>';
                        	if (letter.state == 0) {
                        		html += '<a class="apply-yes btn-normal-orange"><span>同意</span></a></div>';
                        	} else {
                        		html += '<a class="apply-yes btn-normal-orange btn-disabled"><span>已同意</span></a></div>';
                        	}
                        	
                        	var co = letter.content.substring(letter.content.indexOf("。")+1,letter.content.length);
                        	
                        	if (co != null && co != "") {
                        		html += '<div class="apply-word">'+co+'</div>';
                        	}
                        	html += '</div><div class="msg-date"><span>'+letter.cdate.split("T")[0]+" "+letter.cdate.split("T")[1]+'</span></div></div>';
                        }
                    }
                }
                
                $("#letter").html(html);
                
                $('#letter .text').each(function(){
                	$(this).tranText({
                		emotionUrl: '${aier_js_url}trantime/emotions.js'
                	});
                });
                
                $('.msg-item-letter').click(function(){
                	if (!$(this).hasClass('msg-item-forchums')) {
                		var letterUrl = $(this).attr('data-url');
                        window.location = letterUrl;
                	}
				    
				});
                // 更新分页显示
                page = data.page;
                toLoadPage(page, page_size, data.page_count, data.count, "page", "searchLetter", null, null);  
                if (data.count == 0) {
                    $("#page").css("display","none");
                } else {
                    $("#page").css("display","block");
                }
                
                // 同意添加密友
                $('.apply-yes').click(function(){
                	var thisObject = $(this);
                    if(!thisObject.hasClass('btn-disabled')) {
                        var auid= thisObject.closest('.msg-item').attr('data-uid');
                        var type = thisObject.closest('.msg-item').attr('data-type');
                        if (type == 2) {
                        	// 添加密友
                            var fun=function(data){
                                if (data.s == 1) {
                                    thisObject.addClass('btn-disabled').children('span').text('已同意');
                                } else if (data.s == 10001){
                                    $().message("已经是密友！");
                                    thisObject.addClass('btn-disabled').children('span').text('已同意');
                                } else {
                                    $().message("添加失败，请稍后再试！");
                                }
                                 
                            };
                            exeAjax(ajaxUrl("${aier_url}${aier_urijs}userchumscircle_addFriend${aier_suffix}"),
                            fun,{
                                "auid":auid
                           });
                        } else {
                        	// 添加关注
                            var fun=function(data){
                                if (data.s == 1) {
                                    thisObject.addClass('btn-disabled').children('span').text('已同意');
                                } else if (data.s == 10001){
                                    $().message("已经加关注！");
                                    thisObject.addClass('btn-disabled').children('span').text('已同意');
                                } else {
                                    $().message("添加失败，请稍后再试！");
                                }
                                 
                            };
                            exeAjax(ajaxUrl("${aier_url}${aier_urijs}usermyfollow_addFollow${aier_suffix}"),
                            fun,{
                                "fuid":auid
                           });
                        }
                    	
                        
                    }
                });
            }
        };
        exeAjax(
             ajaxUrl("${aier_url}${aier_urijs}usermessage_searchFollowLetter${aier_suffix}"),
             fun,{
             "page":page,
             "page_size":page_size
        });
    }
    
    // 删除所有私信
    $("#delete").click(function(){
    
        $("#deleteConfirm").dialog(
                {
                    //背景变暗
                    modal : true,
                    autoOpen : false,
                    show : "fade",
                    hide : "fade",
                    width : "340",
                    height : "220",
                    buttons : {
                        "确定" : function() {
                            var fun=function(data){
					            if(data.s==1){
					                $("#deleteConfirm").dialog("close");
					                searchLetter(page);
					            } else {
					               $("#deleteConfirm").dialog("close");
					               $().message("删除失败，请稍后再试！");
					            }
					        };
					        exeAjax(
					             ajaxUrl("${aier_url}${aier_urijs}usermessage_deleteLetter${aier_suffix}"),
					             fun,{
					             
					        });
                        },
                        "取消" : function() {
                            $(this).dialog("close");
                        }
                }
            });
            $("#deleteConfirm").dialog("open");
    
    });

    
// 点击按钮发送私信
$('#btnSendALetter').click(function(){
	var dialogObj = $('#dialogSendALetter');
	
	$('.btn-emotion').click(function(){
        
        $(this).sinaEmotion({
            emotionUrl: '${aier_js_url}trantime/emotions.js',
            target : $('#content')
        });
	});

	dialogObj.dialog({
	    //背景变暗
	    modal : true,
	    autoOpen : false,
	    show : "fade",
	    hide : "fade",
	    width : "360",
	    height : "280",
	    buttons : {
	        "确定" : function() {
	            var nickname = $("#nickname").val();
	            var content = $("#content").val();
	            
	            if (nickname == null || nickname.trim().length == 0) {
	               $().message("请输入昵称！");
	               return;
	            }
	            if (content == null || content.trim().length == 0) {
	               $().message("请输入私信内容！");
                   return;
	            }
	            if (content.length > 300) {
	               $().message("私信内容过长！");
                   return;
	            }
	            var fun=function(data){
                    if(data.s==1){
                        dialogObj.dialog("close");
                        searchLetter(page);
                        $("#nickname").val("");
                        $("#content").val("");
                    } else if (data.s = 10001) {
                        $().message("昵称有误，请重新输入！");
                    } else {
                       $().message("发送失败，请稍后再试！");
                    }
                };
                exeAjax(
                     ajaxUrl("${aier_url}${aier_urijs}usermessage_sendLetter${aier_suffix}"),
                     fun,{
                        "nickname":nickname,
                        "content":content
                });  

	        },
	        "取消" : function() {
	        	dialogObj.dialog("close");
	        }
		}
	});
	
	dialogObj.dialog('open');
});

    // 跳转到家校联系薄
    function hsConBook() {
        // 判断是否付费

        // 判断身份（家长或老师）

        // 教师（tid）
        //window.location.href = "${aier_url}user/userhscb_userTeaHSConBook.shtml?tid=2&&cid=1";
        var fun = function(data) {
            if (data.s == 1) {

                if (data.type == 0) {
                    $().message("您目前没有权利进入家校联系薄！");
                    return;
                } else if (data.type == 1 || data.type == 3) {
                    if (data.classList != null && data.classList.length > 0) {
                        window.location.href = "${aier_url}user/userhscb_userTeaHSConBook.shtml?cid="
                                + data.classList[0].cid +"&type="+data.type;
                    } else {
                        window.location.href = "${aier_url}user/userhscb_userTeaHSConBook.shtml?type="+data.type;
                    }

                } else if (data.type == 2) {
                    window.location.href = "${aier_url}user/userhscb_userChiHSConBook.shtml?type="+data.type;
                }

            }
        };
        exeAjax(
                ajaxUrl("${aier_url}${aier_urijs}userhscb_searchClassList${aier_suffix}"),
                fun, {});

    }
    
    

</script>


</body>
</html>